$(function(){
	//页面初始化
	initPage();		
	
	$(".widget-toolbar").on('click',"a:has(.icon-edit)",function(e){
		e.preventDefault();	
		showModal($(this),$("#stepModen"));
	});

//	$('#task-save-form').bootstrapValidator();
	
	//任务新增按钮点击事件
	$(".step-content-cfg").on('click','.task-add-span',function(e){
		var memberArray=null;
		var _this = $(this);
		Otod.ajax({
			url:ctx+"/pm/team/findByPrjId.do?prjId="+prjId,
			contentType: "application/x-www-form-urlencoded;",
			successFn:function(data){
				memberArray = data;
				createTaskAddWrap(_this.attr("key"),memberArray);
			}
		});
		
		return false;
	});
	
	//点击提交项目信息审核
	$("#project-btn-submit").on("click",function(btn){
		showModal(btn,"selectToMember");
	});
	
	//阶段保存
	$("#stepModen-btn-save").on('click',function(){
		
		
		//开始时间
		if ($("#stepModen [name='startDate']").val()) {
			var  stepStartDate = DateUtil.dateToLong(DateUtil.strToDate($("#stepModen [name='startDate']").val()));
			if (stepStartDate < GLOBAL_PRJ.startDate) {
				Otod.Msg.info("阶段开始时间不能小于项目开始时间 当前项目开始时间为"+DateUtil.dateFormat(Number(GLOBAL_PRJ.startDate)));
				return;
			} else if (stepStartDate > GLOBAL_PRJ.endDate) {
				Otod.Msg.info("阶段开始时间不能大于项目结束时间 当前项目开始时间为"+DateUtil.dateFormat(Number(GLOBAL_PRJ.endDate)));
				return;
			}
		} else {
			Otod.Msg.info("阶段开始时间不能为空");
			return;
		} 
		if ($("#stepModen [name='endDate']").val()) {
			var  stepEndDate = DateUtil.dateToLong(DateUtil.strToDate($("#stepModen [name='endDate']").val()));
			if (stepEndDate < GLOBAL_PRJ.startDate) {
				Otod.Msg.info("阶段结束时间不能小于项目开始时间 当前项目开始时间为"+DateUtil.dateFormat(Number(GLOBAL_PRJ.startDate)));
				return;
			} else if (stepEndDate > GLOBAL_PRJ.endDate) {
				Otod.Msg.info("阶段结束时间不能大于项目结束时间 当前项目结束时间为"+DateUtil.dateFormat(Number(GLOBAL_PRJ.endDate)));
				return;
			}
		} else {
			Otod.Msg.info("阶段结束时间不能为空");
			return;
		}

		Otod.ajax({
			url: ctx + '/pm/step/save.do',
			data : $('#stepModen-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			successFn : function(data) {
				$('#stepModen').modal('hide');
				createStepWrap("stepRow",data);
				location.reload();
			}
		});
	});
	
	//审核bu通过
	$("#audit-project-btn-disAgree").on('click',function(){
		
		var comment = $("#audit-project-form [name=comment]").val();
		var agree = "disAgree";
		Otod.ajax({
    		url:ctx +"/pm/comment/audit.do",
    		data: {prjId:prjId,comment:comment,agree:agree},
    		type:'GET',
    		successFn: function(data){
    			$('#audit-project').modal('hide');
	    			Otod.Msg.info(data.info);
    			
    		}
    	 }); 
	});
	
	//审核通过
	$("#audit-project-btn-agree").on('click',function(){
		
		var comment = $("#audit-project-form [name=comment]").val();
		var agree = "agree";
		Otod.ajax({
			url:ctx +"/pm/comment/audit.do",
			data: {prjId:prjId,comment:comment,agree:agree},
			type:'GET',
			successFn: function(data){
				$('#audit-project').modal('hide');
				Otod.Msg.info(data.info);
				
			}
		}); 
	});
	
	
	
	//任务新增保存
	$(".step-content-cfg").on('click',"#task-btn-save",function(e){
//		if(!$('#task-save-form').data('bootstrapValidator').validate().isValid()){
//			return;
//		}
		
		Otod.ajax({
			url: ctx + '/pm/task/insert.do',
			data : $('#task-save-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			successFn : function(data) {
				appendTaskItem(data);
				removeAddTaskWrap();	
				location.reload();
			}
		});
	});
	
	//任务新增取消
	$(".step-content-cfg").on('click',"#task-btn-cancle",function(e){
		removeAddTaskWrap();
	});
	
	//任务删除
	$(".step-content-cfg").on('click',".item-btn-delete",function(e){
		e.preventDefault();
		var taskId = $(this).attr("key");
		Otod.Msg.confirm("确定要删除该任务吗？","删除任务",function(){
			Otod.ajax({
				url: ctx + '/pm/task/delete.do',
				data : taskId,
				successFn : function(data) {

					$("#task-item-panel-"+taskId).remove();
				}
			});
		});
		
	});
	
	
	
	//阶段删除
	$(".step-content-cfg").on('click',".step-btn-delete",function(e){
		e.preventDefault();
		var taskId = $(this).attr("key");
		Otod.Msg.confirm("确定要删除该阶段吗？", "删除阶段",function(){
			Otod.ajax({
				url: ctx + '/pm/step/delete.do',
				data : taskId,
				successFn : function(data) {
					$("#step-wrap-"+taskId).remove();
				}
			});
		});
		
		
	});
	
	//阶段修改
	$(".step-content-cfg").on('click',".step-btn-edit",function(e){
		e.preventDefault();
		editStep(this);
	});
	
	//阶段跳转
	$(".step-content-cfg").on('click',".step-btn-complete",function(e){
		e.preventDefault();
		var stepId = $(this).attr('key');
		Otod.ajax({
			url: ctx + '/pm/step/updateStepComplete.do',
			data : {
				uuid:stepId
			},
			type:"GET",
			successFn : function(data) {
				var responseMsg = data.info;
				if(responseMsg.indexOf("成功") < 0 ){
					Otod.Msg.confirm(responseMsg, "阶段跳转",function(){
						Otod.ajax({
							url: ctx + '/pm/step/updateStepComplete.do',
							data : {
								uuid:stepId,
								forceUpdate:"true"
							},
							type:"GET",
							successFn : function(data) {
								Otod.Msg.info(data.info);
								initPage();
							}
						});
					});
				}else{
					Otod.Msg.info(responseMsg);
					initPage();
				}
			}
		});
		
	});
	
	

	
	
	
	/*生成阶段成果*/
	
	$(".step-content-cfg").on('click',".stepDocCfg",function(e){
		e.preventDefault();
		var stepId = $(this).attr("key");
		var stepName = $(this).attr("stepName");
		//通过uuid生成阶段项目
		selectTaskId($(this),$("#stepDocCfgMtpModen"),stepId,stepName);
	});
	
	
	
	
	
	/**
	 * 显示修改的项目名称的输入框
	 */
	$("#stepProject").on("click","#updateProjectName",function(){
		
		showUpdateProject($(this).attr("key"),$(this).attr("prjName"));
	});
	
	/**
	 * 修改项目名称
	 */
	$("#stepProject").on("click",".saveUpdateProjectName",function(){
		
		saveUpdatePrjName($(this).attr("key"),$("#showUpdatePrjName").val());
	});
	/**
	 * 取消修改项目名称
	 */
	$("#stepProject").on("click",".cancelUpdatePrjName",function(){
		
		cancelUpdatePrjName($(this).attr("key"),$(this).attr("prjName"));
	});
	
	
	
	
	
	/**
	 * 显示修改的项目阶段名称的输入框
	 */
	$("#stepFolderName").on("click","#showUpdateStepFolderName",function(){
		showUpdateStep($(this).attr("key"),$(this).attr("folderName"),$(this).attr("stepId"));
	});
	
	/**
	 * 修改项目某个阶段的名称
	 */
	$("#stepFolderName").on("click",".saveUpdateStepFolderName",function(){
		saveUpdateStepFolderName($(this).attr("key"),$("#showUpdateFolderName").val(),$(this).attr("stepId"));
	});
	/**
	 * 取消修改阶段名称
	 */
	$("#stepFolderName").on("click",".cancelUpdateStepFolderName",function(){
		
		cancelUpdateStepFolderName($(this).attr("key"),$(this).attr("folderName"),$(this).attr("stepId"));
	});
	


	
	
	
	
	/**
	 * 生成文库 保存按钮
	 */
	$("#saveTaskProjectDoc").on("click",function(e){
		//要传个项目id
		saveTaskProjcetDoc(prjId,this.value);
	});
	
	
});
/**
 * 页面初始化
 */
function initPage(){
	Otod.ajax({					
		url : ctx + "/pm/project/viewPrjInfo.do",
		data: prjId,
		successFn:function(data){	
			
			//设置项目类型			
			$('#setPrjName').text(data.prjVo.prjName);
			$('#setprjStatus').text(changeStatus(data.prjVo.status));
			GLOBAL_PRJTYPE = data.prjVo.prjType;		//产品类型
			
			GOBAL_PRJ_ROLE = data.prjRole;			//成员当前项目角色
			GOBAL_PRJ_NAME = data.prjVo.prjName;    //项目名称
			GLOBAL_PRJ = data.prjVo;				//项目信息
			renderStep(data.stepList);				//加载阶段
			renderMember(data.teamList);			//加载人员
			
			initAuth();
			initUserAuth(data.prjVo.status,data.prjVo.createEmpNo);
		}
	});
	initEmpSelect(prjId);
	initEmpSelect2(prjId);
}

//初始化用户权限
function initUserAuth(status,createEmpNo){
	$(".step-btn-create").css("display","none");//项目新建，导入，重载按钮
	$(".step-btn-control").css("display","none");//阶段控制按钮
	$(".task-btn-control").css("display","none");//任务项控制按钮
	$(".taskModen-btn-save").css("display","none");//任务详细内容设置member-btn-control
	$(".member-btn-control").css("display","none");//添加人员设置
	$(".project-btn").css("display","none");//项目提交审核
	$(".project-btn-audit").css("display","none");//项目审核
	$(".step-btn-edit").css("display","none");//阶段修改控制按钮
	$(".step-btn-delete").css("display","none");//阶段删除控制按钮
	$("#control-sort").css("display","none");//阶段排序字段控制
	if(GLOBAL_PRJ.status =="30090001"&&GOBAL_PRJ_ROLE=='30100002'){//项目准备中
		//项目负责人
			$(".step-btn-create").css("display","");//项目新建，导入，重载按钮
			$(".step-btn-control").css("display","none");//阶段控制按钮
			$(".task-btn-control").css("display","");//任务项控制按钮
			$(".taskModen-btn-save").css("display","");//任务详细内容设置member-btn-control
			$(".member-btn-control").css("display","");//添加人员设置
			$(".project-btn").css("display","");//项目提交审核
			$(".step-btn-edit").css("display","");//阶段修改控制按钮
			$(".step-btn-delete").css("display","");//阶段删除控制按钮
		
	}else if(GLOBAL_PRJ.status =="30090005"||GLOBAL_PRJ.status == "30090001"){//项目提交审核
		if(GOBAL_PRJ_ROLE !='30100002'&&GOBAL_PRJ_ROLE !='30100003'&&GOBAL_PRJ_ROLE !='30100007'){
			$(".project-btn-audit").css("display","");//项目审核
		}
		if(GOBAL_PRJ_ROLE=='30100002'){
			$(".project-btn").css("display","");//项目提交审核
		}
	}else if(GLOBAL_PRJ.status =="30090002"&&GOBAL_PRJ_ROLE=='30100002'){
		
		$(".task-btn-control").css("display","none");//任务项添加任务控制按钮
		$(".step-btn-control").css("display","");//阶段控制按钮
		$(".step-btn-edit").css("display","none");//阶段修改控制按钮
		$(".step-btn-delete").css("display","none");//阶段删除控制按钮
		
	}
	
	if(GOBAL_PRJ_ROLE =='30100007'){
		if(GLOBAL_PRJ.status !='30090001'&&GLOBAL_PRJ.status !='30090004'){
			$(".taskModen-btn-save").css("display","");//任务详细内容设置member-btn-control
			$(".member-btn-control").css("display","");//添加人员设置
			$(".step-btn-edit").css("display","");//阶段修改控制按钮
			$(".step-btn-delete").css("display","");//阶段删除控制按钮
			$(".step-btn-create").css("display","");//项目新建，导入，重载按钮
			$(".task-btn-control").css("display","");//任务项控制按钮
		}
	}
	
	
}




/*
 * 初始化权限
 */
function initAuth(){
	//$(".u-step .widget-header"). bg-white
	$(".task-item").each(function(i){
		var empNo = $(this).attr("empNo");
		if(GOBAL_CUST_NO == empNo){
			$(this).children(".item-list-status").addClass("bg-blue");
		}
	});
	
	$(".u-step").each(function(i){
		if($(this).attr("status") == "30050002"){	//进行中任务
			$(this).find(".widget-header").addClass("bg-white");
		}
	});
}



//显示弹出选项框
function showEditStepWin(){
	return false;
}

//渲染阶段
function renderStep(data){
	$("#stepRow").html("");
	$.each(data,function(i,o){
		createStepWrap("stepRow",o);
	});
}


//加载阶段
function loadStep(){
	Otod.ajax({					//查询已存在列表
		url : ctx + "/pm/step/list.do",
		data: prjId,
		successFn:function(data){
			renderStep(data);
		}
	});
}












//插入式新增step
function insertStep(btn){
	resetStepModen();
	
	showModal(btn,"stepModen");
	$("#control-sort").css("display","");//阶段排序字段控制
}
//新增step
function addStep(btn){
	resetStepModen();
	
	showModal(btn,"stepModen");
}


//auditProject
function auditProject(btn){
	$("#audit-project-form [name=prjId]").val(prjId);		//初始化prjId
	$("#audit-project-form [name=projectName]").val(GOBAL_PRJ_NAME);		//初始化prjId
	showModalFollowRight(btn,"audit-project");
}




//修改step
function editStep(btn){
	resetStepModen();
	$("#stepModen .modal-title").text("修改阶段");
	$("#stepModen [name='uuid']").val($(btn).attr("key"));
	$("#stepModen [name='stepName']").val($(btn).attr("stepName"));
	
	//阶段开始时间
	if ($(btn).attr("startDate")) {
		$("#stepModen [name='startDate']").val(DateUtil.dateFormat(Number($(btn).attr("startDate"))));
	}
	//阶段开始时间
	if ($(btn).attr("endDate")) {
		$("#stepModen [name='endDate']").val(DateUtil.dateFormat(Number($(btn).attr("endDate"))));
	}
	
	
	showModalFollowRightStep(btn,"stepModen");
}

//重置step moden
function resetStepModen(){
	$("#stepModen-form")[0].reset();
	$("#stepModen-form [name=prjId]").val(prjId);		//初始化prjId
}

//显示modal
function showModal(btn,modal){
	var modalObj = getJqueryObj(modal);
	
	modalObj.modal();
	setModelFlowBtn(modalObj.children(".modal-dialog").get(0),btn);
}

//右侧显示modal
function showModalFollowRightStep(btn,modal){
	var modalObj = getJqueryObj(modal);
	
	modalObj.modal();
	setModelFollowRight(modalObj,"300px");
	//$('.chosen-container-multi').css({'width':'270px','height':'30px'});
}
function showModalFollowRight(btn,modal){
	var modalObj = getJqueryObj(modal);
	
	modalObj.modal();
	setModelFollowRight(modalObj,"500px");
	$('.chosen-container-multi').css({'width':'270px','height':'30px'});
}

//显示modal
function showTpModal(modal){
	var modalObj = getJqueryObj(modal);
	modalObj.modal();
}





/*小组件*******************************************/

//创建阶段列表包
function createStepWrap(appendTo,data){
	var stepMtp = $('#stepMtp').html();
	Mustache.parse(stepMtp);
	
	var taskItemMtp = $('#taskItemLinkMtp').html();
	//o.uuid,o.stepName,o.taskList
	var rendered = Mustache.render(stepMtp, {
		data: data, 
		isStartingStep:function(){
			return this.status == '30060002';
		},
		isCompleteStep:function(){
			return this.status != '30060003';
		},
		taskStatusStyle: function(){
			if(this.status == "30050002"){			//进行中
				return "blue";
			}else if(this.status == "30050003"){	//待确认
				return "orange";
			}else if(this.status == "30050004"){	//完成
				return "green";
			}else if(this.status == "30050005"){	//被退回
				return "red";
			}else{									//未开始
				return "";
			}
		},
		taskStatusName: function(){
			if(this.status == "30050002"){			//进行中
				return "进行中";
			}else if(this.status == "30050003"){	//待确认
				return "待确认";
			}else if(this.status == "30050004"){	//完成
				return "完成";
			}else if(this.status == "30050005"){   //被退回
				return "被退回";
			}else{									//未开始
				return "未开始";
			}
		},
		empNames: function(){
			var empNameList = this.empList;
			var empNamesList = "";
			if(this.empList.length>0){
				for(var i=0;i<empNameList.length;i++){
					empNamesList = empNamesList + empNameList[i].empName+",";
				}
				return  empNamesList.substring(0,empNamesList.length-1);
			}
		}
	},{taskItemLink:taskItemMtp});
	$('#'+appendTo).append(rendered);
}

//删除任务新增页面
function removeAddTaskWrap(){
	$(".item-task-add").remove();
}

//创建任务新增页面
function createTaskAddWrap(stepId,memberArray){
	removeAddTaskWrap();
	var taskAddMtp = $('#taskAddMtp').html();
	Mustache.parse(taskAddMtp);
	
	var taskMemberListMtp = $('#taskMemberListMtp').html();
	
	var rendered = Mustache.render(taskAddMtp, {stepId: stepId,prjId:prjId,memberArray: memberArray},{taskMemberListMtp:taskMemberListMtp});
	
	$("#step-wrap-ul-"+stepId).append(rendered);
	//绑定datepicker
	$('#step-wrap-ul-'+ stepId + ' .div-datepicker').datepicker()
		.on('changeDate', function(ev){
		var jt = $(ev.target);
		alert(jt.data('date'));
		jt.children('span').text(jt.data('date'));
		//$("#task-save-form [name='endDate']").attr("value",jt.data('date'));
		jt.datepicker('hide');
	});
	
	$("#divTaskType").append($("#tagsSelect").html());
}

//添加任务 item
function appendTaskItem(taskObj){
	var html = createTaskItemHtml(taskObj);
	$("#step-wrap-ul-"+taskObj.stepId).append(html);
}
//生成任务 item string
function createTaskItemHtml(taskObj){
	var template = $('#taskItemMtp').html();
	Mustache.parse(template);
	
	var taskItemLinkMtp = $('#taskItemLinkMtp').html();
	
	var rendered = Mustache.render(template, {
		data:taskObj,
		taskStatusStyle: function(){
			if(this.status == "30050002"){			//进行中
				return "blue";
			}else if(this.status == "30050003"){	//待确认
				return "orange";
			}else if(this.status == "30050004"){	//完成
				return "green";
			}else if(this.status == "30050005"){		//被退回							
				return "red";
			}else{
				return ""; //未开始
			}
		},
		taskStatusName: function(){
			if(this.status == "30050002"){			//进行中
				return "进行中";
			}else if(this.status == "30050003"){	//待确认
				return "待确认";
			}else if(this.status == "30050004"){	//完成
				return "完成";
			}else if(this.status == "30050005"){   //被退回
				return "被退回";
			}else{									//未开始
				return "未开始";
			}
		},
		empNames: function(){
			var empNameList = this.empList;
			var empNamesList = "";
			if(this.empList!=null&&this.empList.length>0){
				for(var i=0;i<empNameList.length;i++){
					empNamesList = empNamesList + empNameList[i].empName+",";
				}
				return  empNamesList.substring(0,empNamesList.length-1);
			}
		}
	},{taskItemLink:taskItemLinkMtp});
	return rendered;
}


//refresh task
function refreshTask(taskObj){
	var $taskItemPanel = $("#task-item-panel-"+taskObj.uuid);
	$(createTaskItemHtml(taskObj)).insertAfter("#task-item-panel-"+taskObj.uuid);
	$taskItemPanel.remove();
}
/*********************************生成阶段成果*************************************************/

//通过阶段taskId查询数据
function selectTaskId(btn,model,stepId,stepName){	
	 $("#saveTaskProjectDoc").val(stepId);
	 $.fn.zTree.destroy();
	 $("#showStepProject").html();
	 $("#stepFolderName").html("");
	 $("#treeRight").html("");
	 $("#saveCfgStepId").val(stepId);
     $("#saveCfgStepName").val(stepName);
     //增加右边的目录treeRigthAdd
     $("#treeRightAdd").on("click",{isParent:true},addRight);
    	 
     $("#treeRightClear").on("click",{isParent:true}, clearRight);
     $("#combineSelectedWord").on("click",{isParent:true}, combineSelectedWord);
    
     //显示 页面
	 showDocCfgMtpModen(btn,model);
	 
	 //查询项目名称
	 selectProjectName();
	 
	 //查询阶段名称
	 selectStepFolder();
	 //显示节点
     addTreeLeftOrRight();  
    
}

//显示生成阶段成果模板modal
function showDocCfgMtpModen(btn,modal){
	
	var modalObj = getJqueryObj(modal);
	
	modalObj.modal();
	/*设置位置*/
	//setModelFlowBtn(modalObj.children(".modal-dialog").get(0),btn);
}

//查询项目名称 如果有就不创建 没有就创建
function selectProjectName(){

	Otod.ajax({
		url : ctx+"/pm/pmStepDocCfg/selectProject.do",	
		data:{uuid:prjId},
		type:"GET",
		successFn:function(data){			
			showStepProject(data.uuid,data.folderName);
		}
	});
}



//显示项目名称
function showStepProject(uuid,prjName){
	
	var showStepProject = $('#showStepProject').html();
	Mustache.parse(showStepProject);
	var rendered = Mustache.render(showStepProject,{
		uuid:uuid,
		prjName:prjName
	});
	$("#stepProject").html(rendered);	
}

/**
 *显示修改的项目名称的输入框
 * @param prjId 项目的id
 * @param prjName  项目的名称
 */
function showUpdateProject(prjId,prjName){
	
	var showUpdateProjectName = $("#showUpdateProjectName").html();
	Mustache.parse(showUpdateProjectName);
	var rendered = Mustache.render(showUpdateProjectName,{
		prjId:prjId,
		prjName:prjName
	});
	$("#stepProject").html(rendered);
}

/**
 * 修改项目名称
 * @param prjId 项目id
 * @param prjName  项目名称
 */
function saveUpdatePrjName(prjId,prjName){
	
	Otod.ajax({
		url : ctx+"/pm/pmStepDocCfg/updateProjectName.do",
		data:{uuid:prjId,folderName:prjName},
		type:"GET",
		successFn:function(data){	
			cancelUpdatePrjName(prjId,prjName);
			
		}
	});
}

/**
 * 取消修改
 * @param prjId  项目id
 * @param prjName  项目名称
 */
function cancelUpdatePrjName(prjId,prjName){
	
	showStepProject(prjId,prjName);
}

//查询是否存在这个阶段的文件夹 不存在则创建
function selectStepFolder(){
	Otod.ajax({
		url : ctx+"/pm/pmStepDocCfg/selectStepFoder.do",
		data:{uuid:$("#saveCfgStepId").val(),folderName:$("#saveCfgStepName").val(),parentId:prjId},
		type:"GET",
		successFn:function(data){
			showStepFolderName(data.uuid,data.folderName,data.stepId);
		}
	});
}


//显示阶段文件夹
function showStepFolderName(uuid,folderName,stepId){
	var showStepFolder = $('#showStepfolder').html();
	Mustache.parse(showStepFolder);
	var rendered = Mustache.render(showStepFolder,{
		stepId:stepId,
		folderName:folderName,
		uuid:uuid
		
	});
	$("#stepFolderName").html(rendered);	
}


/**
 *显示修改的阶段名字的输入框
 * @param prjId 项目的id
 * @param prjName  项目的名称
 */
function showUpdateStep(uuid,folderName,stepId){
	
	var showUpdateStep = $("#showUpdateStep").html();
	Mustache.parse(showUpdateStep);
	var rendered = Mustache.render(showUpdateStep,{
		uuid:uuid,
		folderName:folderName,
		stepId:stepId
	});
	$("#stepFolderName").html(rendered);
}



/**
 * 修改项目某个阶段的名称
 * @param prjId 项目id
 * @param prjName  项目名称
 */
function saveUpdateStepFolderName(uuid,folderName,stepId){
	Otod.ajax({
		url : ctx+"/pm/pmStepDocCfg/updateProjectName.do",
		data:{uuid:uuid,folderName:folderName,stepId:stepId},
		type:"GET",
		successFn:function(data){	
			cancelUpdateStepFolderName(uuid,folderName,stepId);
			
		}
	});
}

/**
 * 取消修改项目某个阶段
 * @param prjId  项目id
 * @param prjName  项目名称
 */
function cancelUpdateStepFolderName(uuid,folderName,stepId){
	showStepFolderName(uuid,folderName,stepId);
}


//增加左边的数据
function addTreeLeftOrRight(){

	//得到阶段弹出页面数据 
    Otod.ajax({
		url : ctx+"/pm/task/selectTaskId.do",
		data:{stepId: $("#saveCfgStepId").val()},
		type:"GET",
		successFn:function(listData){
			for(var i = 0;i<listData.length;i++){
				//文件夹						
				if(listData[i].folderType=="40010001"){
					listData[i].icon = ctx +"/static/images/folder.png";
				}else{
					listData[i].icon=  ctx +"/static/images/file.png";
				}
			}
			//加载数据左边
			 $.fn.zTree.init($("#treeLeft"),setting,listData);			
			
		}
	});
    
    
  //得到右边的数据
    Otod.ajax({
    	url : ctx+"/pm/pmStepDocCfg/queryForList.do",
		data:{stepId: $("#saveCfgStepId").val()},
		type:"GET",
		successFn:function(data){
			for(var i = 0;i<data.length;i++){
				//文件夹						
				if(data[i].folderType=="40010001"){
					data[i].icon = ctx +"/static/images/folder.png";
				}else{
					data[i].icon=  ctx +"/static/images/file.png";
				}
			}
			//加载数据			
			 $.fn.zTree.init($("#treeRight"),settingRight,data);
			// $("#selectAll").bind("click", selectAll);			
			// selectStepFolder();
		}
    });
   
}






//增加父亲节点右边
function addRight(e) {
	
	var pId = $("#saveCfgStepId").val();
	
	var zTree = $.fn.zTree.getZTreeObj("treeRight"),
	isParent = e.data.isParent,
	nodes = zTree.getSelectedNodes(),
	treeNode = nodes[0];	
	
	
	if(treeNode==undefined){
		if (treeNode) {
			treeNode = zTree.addNodes(treeNode, {uuid:getRadom(), parentId:treeNode.uuid, isParent:isParent, folderType:"40010001",folderName:"新建文件夹" + (newCount++)});
		}else {		
			treeNode = zTree.addNodes(null, {uuid:getRadom(),parentId:pId,isParent:isParent, folderType:"40010001",folderName:"新建文件夹" + (newCount++)});
		}
		if (treeNode) {	
			zTree.editName(treeNode[0]);
		} else {
			alert("叶子节点被锁定，无法增加子节点");
		}
	}
	
	
};
//清除选中的节点右边边
function clearRight(e){
	
	var zTree = $.fn.zTree.getZTreeObj("treeRight");
	var nodes = zTree.getNodes();	
	for(var i =0;i<nodes.length;i++){
		eachClearChildNodes(nodes[i],zTree);
	}
} 
//将选中的文档计入成果文档
function combineSelectedWord(e){
	
	var zTree = $.fn.zTree.getZTreeObj("treeRight");
	var nodes = zTree.getSelectedNodes();	
	var wordIds = new Array();
	var stepId = $("#saveCfgStepId").val();
	for(var i =0;i<nodes.length;i++){
		if(nodes[i].isParent){
			
		}else{
			wordIds.push(nodes[i].uuid);
			
		}
	}
	if(wordIds.length>0){
		Otod.ajax({
			url : ctx+"/office/region/create.do",
			data:{wordIds:wordIds.join(","),stepId:stepId,prjId:prjId,},
			type:'GET',
			successFn:function(data){
				Otod.Msg.info(data.info);
			}
		});
		//window.open(ctx+"/office/region/create.do?wordIds="+wordIds.join(",")+"&stepId="+stepId+"&prjId="+prjId);
	}else{
		Otod.Msg.info("请在右侧框内先选择需要计入成果的文档！");
	}
	//window.location.href=ctx+"/office/region/create.do?wordIds="+wordIds.join(",")+"&stepId="+stepId;
	
} 
//清除所有选中的节点
function eachClearChildNodes(node,treeObj){
	
	treeObj.cancelSelectedNode(node);
	if(node.children==undefined) return;
	for(var i=0;i<node.children.length;i++){
		eachClearChildNodes(node.children[i],treeObj);
	}
}

//添加文件 或者文件 修改文件 或者文件名字
function addFolderOrFile(uuid,stepId,folderType,folderName,parentId){
	
	
	if(parentId==null){
		parentId =  $("#saveCfgStepId").val();
	}
	
	
	Otod.ajax({
		url : ctx+"/pm/pmStepDocCfg/addFolderOrFile.do",
		data:{uuid:uuid,stepId:stepId,folderType:folderType,folderName:folderName,parentId:parentId},
		type:'GET',
		successFn:function(data){
			
		}
	});
}
//合并选中的文档
function combineSelectWord(){
	
	
	if(parentId==null){
		parentId =  $("#saveCfgStepId").val();
	}
	
	//得到选中的节点
	var zTree = $.fn.zTree.getZTreeObj("treeLeft");
	var nodes = zTree.getSelectedNodes();
	
	Otod.ajax({
		url : ctx+"/pm/pmStepDocCfg/addFolderOrFile.do",
		data:{uuid:uuid,stepId:stepId,folderType:folderType,folderName:folderName,parentId:parentId},
		type:'GET',
		successFn:function(data){
			
		}
	});
}



//获取某个节点下的所有子节点

function getAllChildrenNodes(treeNode,result){
      if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
            	
                result += ',' + childrenNodes[i].uuid;
                result = getAllChildrenNodes(childrenNodes[i], result);
            }
        }
    }
    return result;
}


/**
 * 得到所有删除节点的id
 * @param uuid
 */
function deletFileOrFolder(uuid){
	Otod.ajax({
		url:ctx+"/pm/pmStepDocCfg/deleteFolderOrFile.do",
		data:{uuid:uuid},
		type:'GET',
		successFn:function(data){
			addTreeLeftOrRight();
			
		}
	});
}

/**
 * 查询名字是否重复
 * @param folderName 文件夹名字
 * @param parentId  上级文件夹的id
 */
function selectRepetitionFolderName(folderName,parentId){
	
	var id;
	Otod.ajax({
		url:ctx+"/pm/pmStepDocCfg/selectRepetitionFolderName.do",
		data:{folderName:folderName,parentId:parentId,stepId:$("#saveCfgStepId").val()},
		type:'GET',
		async: false,
		successFn:function(data){	
			id=data.uuid;
		}
		
	});	
	return id;
	
}

/**
 * 向文库表添加数据
 * @param prjId
 */
function saveTaskProjcetDoc(prjId,stepId){
	Otod.ajax({
		url:ctx+'/pm/pmStepDocCfg/addProjectDoc.do',
		data:{prjId:prjId,stepId:stepId},
		type:'GET',
		successFn:function(data){
			Otod.Msg.info("添加项目文库成功");
		}
	});
}

function changeStatus(status){
	if(status=="30090001"){
		return "准备中";
	}else if(status=="30090002"){
		return "进行中";
	}else if(status=="30090003"){
		return "中止";
	}else if(status=="30090004"){
		return "结项";
	}else if(status=="30090005"){
		return "提交审核";
	}
}




